package utiles_isi;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class isi_conexion {
	private Connection conexion;
	public Statement st;
	public boolean conectado;
	private String driver_;
	private String user_;
	private String pass_;
	
	
	public isi_conexion(String driver_, String user_, String pass_) {
		super();
		this.driver_ = driver_;
		this.user_ = user_;
		this.pass_ = pass_;
	}
	public isi_conexion(String file_properties) {
		super();
		Properties prop ;
		prop = new Properties();
		if(file_properties.isEmpty())file_properties="db.properties";
		 try {
			 
			 prop.load(new FileInputStream(file_properties));
			 this.driver_=prop.getProperty("driver", "jdbc:mysql://127.0.0.1:3306/isitrader");
			 this.user_=prop.getProperty("user", "root");
			 this.pass_=prop.getProperty("pass", "17c50aaipmp");
			 
			 
			// Guarda();
			 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 
	}
    
	public boolean Active(){
		return conectado;
	}
	
	public void conecta(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
		    conexion=DriverManager.getConnection(driver_, user_,pass_);
		    st = conexion.createStatement();
		    conectado=true;
		   
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			conectado=false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			conectado=false;
		}
		
		
	}
	public void Guarda()
	{
		Properties prop ;
		prop = new Properties();
		prop.setProperty("driver", this.driver_);
		prop.setProperty("user", this.user_);
		prop.setProperty("pass", this.pass_);
		
		try {
			prop.store(new FileOutputStream("db.properties"), null);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void desconecta(){
	
		try {
			st.close();
			conexion.close();
			conectado=false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			conectado=false;
		}
	}
	
	
}
